//
//  NSFileManager+QTAdd.m
//  Pods
//
//  Created by 张俊博 on 16/8/23.
//
//

#import "NSFileManager+QTAdd.h"
#import "QTCategoryMacro.h"
#import "NSUserDefaults+QTAdd.h"
#import "NSDate+QTAdd.h"

QTSYNTH_DUMMY_CLASS(NSFileManager_QTAdd)

@interface NSUserDefaults (File)

@end

@implementation NSUserDefaults (File)

+ (BOOL)addDeleteDomain:(NSString *)domainId {
    NSMutableArray *domians = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"kDelDomain"]];
    if (!domians) {
        domians = [NSMutableArray array];
    }
    [domians addObject:domainId];
    [[NSUserDefaults standardUserDefaults] setValue:domians
                                             forKey:@"kDelDomain"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    return YES;
}

+ (NSArray *)deleteDomains {
    NSMutableArray *domians = [[NSUserDefaults standardUserDefaults] objectForKey:@"kDelDomain"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"kDelDomain"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    return domians;
}


@end

@implementation NSFileManager (QTAdd)

#pragma mark - User

+ (NSString *)userDirectory:(NSString *)accoutId
{
    NSString *docsdir = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    docsdir = [docsdir stringByAppendingPathComponent:accoutId];
    
    BOOL isDir;
    BOOL exit =[fileManager fileExistsAtPath:docsdir isDirectory:&isDir];
    if (!exit || !isDir) {
        [fileManager createDirectoryAtPath:docsdir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return docsdir;
}

+ (NSString *)userDic
{
    return [NSFileManager userDirectory:[NSUserDefaults accountID]];
}

#pragma mark - Domain

+ (NSString *)domainDirectory:(NSString *)accoutId domain:(NSString *)domainId
{
    NSString *docsdir = [self userDirectory:accoutId];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    docsdir = [docsdir stringByAppendingPathComponent:domainId];
    
    BOOL isDir;
    BOOL exit =[fileManager fileExistsAtPath:docsdir isDirectory:&isDir];
    if (!exit || !isDir) {
        [fileManager createDirectoryAtPath:docsdir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return docsdir;
}

+ (NSString *)domainDic:(NSString *)domainId
{
    return [NSFileManager domainDirectory:[NSUserDefaults accountID] domain:domainId];
}

+ (NSString *)domainDic
{
    return [NSFileManager domainDirectory:[NSUserDefaults accountID] domain:[NSUserDefaults domainID]];
}

#pragma mark - Official Avatar

+ (NSString *)officialAvatarDirectory:(NSString *)accoutId domain:(NSString *)domainId
{
    NSString *docsdir = [self domainDirectory:accoutId domain:domainId];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    docsdir = [docsdir stringByAppendingPathComponent:@"OfficialAvatar"];
    
    BOOL isDir;
    BOOL exit =[fileManager fileExistsAtPath:docsdir isDirectory:&isDir];
    if (!exit || !isDir) {
        [fileManager createDirectoryAtPath:docsdir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return docsdir;
}

+ (NSString *)officialAvatarDic:(NSString *)domainId
{
    return [NSFileManager officialAvatarDirectory:[NSUserDefaults accountID] domain:domainId];
}

+ (NSString *)officialAvatarDic
{
    return [NSFileManager officialAvatarDirectory:[NSUserDefaults accountID] domain:[NSUserDefaults domainID]];
}

#pragma mark - File

+ (NSString *)fileDirectory:(NSString *)accoutId domain:(NSString *)domainId
{
    NSString *docsdir = [self domainDirectory:accoutId domain:domainId];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    docsdir = [docsdir stringByAppendingPathComponent:@"File"];
    
    BOOL isDir;
    BOOL exit =[fileManager fileExistsAtPath:docsdir isDirectory:&isDir];
    if (!exit || !isDir) {
        [fileManager createDirectoryAtPath:docsdir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return docsdir;
}

+ (NSString *)fileDic:(NSString *)domainId
{
    return [NSFileManager fileDirectory:[NSUserDefaults accountID] domain:domainId];
}

+ (NSString *)fileDic
{
    return [NSFileManager fileDirectory:[NSUserDefaults accountID] domain:[NSUserDefaults domainID]];
}

+ (NSString *)fileIdDic:(NSString *)accoutId domain:(NSString *)domainId fileId:(NSString *)fileId
{
    NSString *fileDic = [NSFileManager fileDirectory:accoutId domain:domainId];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    fileDic = [fileDic stringByAppendingPathComponent:fileId];
    
    BOOL isDir;
    BOOL exit =[fileManager fileExistsAtPath:fileDic isDirectory:&isDir];
    if (!exit || !isDir) {
        [fileManager createDirectoryAtPath:fileDic withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return fileDic;
}

+ (NSString *)fileIdDic:(NSString *)domainId fileId:(NSString *)fileId
{
    return [NSFileManager fileIdDic:[NSUserDefaults accountID] domain:domainId fileId:fileId];
}

+ (NSString *)fileIdDic:(NSString *)fileId
{
    return [NSFileManager fileIdDic:[NSUserDefaults accountID] domain:[NSUserDefaults domainID] fileId:fileId];
}

+ (NSString *)fileDateDic:(NSString *)accoutId domain:(NSString *)domainId
{
    NSString *fileDic = [NSFileManager fileDirectory:accoutId domain:domainId];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    fileDic = [fileDic stringByAppendingPathComponent:[[NSDate date] stringWithFormat:@"yyyy-MM-dd HH:mm:ss.SSS"]];
    
    BOOL isDir;
    BOOL exit =[fileManager fileExistsAtPath:fileDic isDirectory:&isDir];
    if (!exit || !isDir) {
        [fileManager createDirectoryAtPath:fileDic withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return fileDic;
}

+ (NSString *)fileDateDic:(NSString *)domainId
{
    return [NSFileManager fileDateDic:[NSUserDefaults accountID] domain:domainId];
}

+ (NSString *)fileDateDic
{
    return [NSFileManager fileDateDic:[NSUserDefaults accountID] domain:[NSUserDefaults domainID]];
}

#pragma mark - Voice

+ (NSString *)voiceDirectory:(NSString *)accoutId domain:(NSString *)domainId
{
    NSString *docsdir = [self domainDirectory:accoutId domain:domainId];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    docsdir = [docsdir stringByAppendingPathComponent:@"Voice"];
    
    BOOL isDir;
    BOOL exit =[fileManager fileExistsAtPath:docsdir isDirectory:&isDir];
    if (!exit || !isDir) {
        [fileManager createDirectoryAtPath:docsdir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return docsdir;
}

+ (NSString *)voiceDic:(NSString *)domainId
{
    return [NSFileManager voiceDirectory:[NSUserDefaults accountID] domain:domainId];
}

+ (NSString *)voiceDic
{
    return [NSFileManager voiceDirectory:[NSUserDefaults accountID] domain:[NSUserDefaults domainID]];
}

+ (NSString *)voiceIdDic:(NSString *)accoutId domain:(NSString *)domainId fileId:(NSString *)fileId
{
    NSString *fileDic = [NSFileManager voiceDirectory:accoutId domain:domainId];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    fileDic = [fileDic stringByAppendingPathComponent:fileId];
    
    BOOL isDir;
    BOOL exit =[fileManager fileExistsAtPath:fileDic isDirectory:&isDir];
    if (!exit || !isDir) {
        [fileManager createDirectoryAtPath:fileDic withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return fileDic;
}

+ (NSString *)voiceIdDic:(NSString *)domainId fileId:(NSString *)fileId
{
    return [NSFileManager voiceIdDic:[NSUserDefaults accountID] domain:domainId fileId:fileId];
}

+ (NSString *)voiceIdDic:(NSString *)fileId
{
    return [NSFileManager voiceIdDic:[NSUserDefaults accountID] domain:[NSUserDefaults domainID] fileId:fileId];
}

+ (NSString *)voiceDateDic:(NSString *)accoutId domain:(NSString *)domainId
{
    NSString *fileDic = [NSFileManager voiceDirectory:accoutId domain:domainId];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    fileDic = [fileDic stringByAppendingPathComponent:[[NSDate date] stringWithFormat:@"yyyy-MM-dd HH:mm:ss.SSS"]];
    
    BOOL isDir;
    BOOL exit =[fileManager fileExistsAtPath:fileDic isDirectory:&isDir];
    if (!exit || !isDir) {
        [fileManager createDirectoryAtPath:fileDic withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return fileDic;
}

+ (NSString *)voiceDateDic:(NSString *)domainId
{
    return [NSFileManager voiceDateDic:[NSUserDefaults accountID] domain:domainId];
}

+ (NSString *)voiceDateDic
{
    return [NSFileManager voiceDateDic:[NSUserDefaults accountID] domain:[NSUserDefaults domainID]];
}

#pragma mark - Image

+ (NSString *)imageDirectory:(NSString *)accoutId domain:(NSString *)domainId
{
    NSString *docsdir = [self domainDirectory:accoutId domain:domainId];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    docsdir = [docsdir stringByAppendingPathComponent:@"Image"];
    
    BOOL isDir;
    BOOL exit =[fileManager fileExistsAtPath:docsdir isDirectory:&isDir];
    if (!exit || !isDir) {
        [fileManager createDirectoryAtPath:docsdir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return docsdir;
}

+ (NSString *)imageDic:(NSString *)domainId
{
    return [NSFileManager imageDirectory:[NSUserDefaults accountID] domain:domainId];
}

+ (NSString *)imageDic
{
    return [NSFileManager imageDirectory:[NSUserDefaults accountID] domain:[NSUserDefaults domainID]];
}

+ (NSString *)imageIdDic:(NSString *)accoutId domain:(NSString *)domainId fileId:(NSString *)fileId
{
    NSString *fileDic = [NSFileManager imageDirectory:accoutId domain:domainId];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    fileDic = [fileDic stringByAppendingPathComponent:fileId];
    
    BOOL isDir;
    BOOL exit =[fileManager fileExistsAtPath:fileDic isDirectory:&isDir];
    if (!exit || !isDir) {
        [fileManager createDirectoryAtPath:fileDic withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return fileDic;
}

+ (NSString *)imageIdDic:(NSString *)domainId fileId:(NSString *)fileId
{
    return [NSFileManager imageIdDic:[NSUserDefaults accountID] domain:domainId fileId:fileId];
}

+ (NSString *)imageIdDic:(NSString *)fileId
{
    return [NSFileManager imageIdDic:[NSUserDefaults accountID] domain:[NSUserDefaults domainID] fileId:fileId];
}

+ (NSString *)imageDateDic:(NSString *)accoutId domain:(NSString *)domainId
{
    NSString *fileDic = [NSFileManager imageDirectory:accoutId domain:domainId];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    fileDic = [fileDic stringByAppendingPathComponent:[[NSDate date] stringWithFormat:@"yyyy-MM-dd HH:mm:ss.SSS"]];
    
    BOOL isDir;
    BOOL exit =[fileManager fileExistsAtPath:fileDic isDirectory:&isDir];
    if (!exit || !isDir) {
        [fileManager createDirectoryAtPath:fileDic withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return fileDic;
}

+ (NSString *)imageDateDic:(NSString *)domainId
{
    return [NSFileManager imageDateDic:[NSUserDefaults accountID] domain:domainId];
}

+ (NSString *)imageDateDic
{
    return [NSFileManager imageDateDic:[NSUserDefaults accountID] domain:[NSUserDefaults domainID]];
}

#pragma mark - Avatar

+ (NSString *)avatarDirectory:(NSString *)accoutId domain:(NSString *)domainId
{
    NSString *docsdir = [self domainDirectory:accoutId domain:domainId];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    docsdir = [docsdir stringByAppendingPathComponent:@"Avatar"];
    
    BOOL isDir;
    BOOL exit =[fileManager fileExistsAtPath:docsdir isDirectory:&isDir];
    if (!exit || !isDir) {
        [fileManager createDirectoryAtPath:docsdir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return docsdir;
}

+ (NSString *)avatarDic:(NSString *)domainId
{
    return [NSFileManager avatarDirectory:[NSUserDefaults accountID] domain:domainId];
}

+ (NSString *)avatarDic
{
    return [NSFileManager avatarDirectory:[NSUserDefaults accountID] domain:[NSUserDefaults domainID]];
}

+ (BOOL)deleteDomainDic:(NSString *)domainId includeSql:(BOOL)includeSql
{
    NSString *domainDic = [NSFileManager domainDic:domainId];
    if (includeSql) {
        return [[NSFileManager defaultManager] removeItemAtPath:domainDic error:nil];
    }
    [NSUserDefaults addDeleteDomain:domainId];
    NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:domainDic error:nil];
    NSString *filePath = nil;
    NSError *error = nil;
    for (NSString *subPath in subPathArr) {
        if ([subPath.pathExtension isEqualToString:@"sqlite"]) {
            continue;
        }
        filePath = [domainDic stringByAppendingPathComponent:subPath];
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
        if (error) {
            return NO;
        }
    }
    return YES;
}

+ (BOOL)deleteDomainsWithout:(NSArray *)currentDomains
{
    NSMutableArray *delDomains = [NSMutableArray arrayWithArray:[NSUserDefaults deleteDomains]];
    [delDomains removeObjectsInArray:currentDomains];
    for (NSString *domainId in delDomains) {
        [self deleteDomainDic:domainId includeSql:YES];
    }
    return YES;
}

#pragma mark - Caches

+ (NSString *)cachesDic
{
    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"QingTui"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir;
    BOOL exit =[fileManager fileExistsAtPath:path isDirectory:&isDir];
    if (!exit || !isDir) {
        [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return path;
}

+ (BOOL)clearCaches
{
    NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self cachesDic] error:nil];
    NSString *filePath = nil;
    NSError *error = nil;
    for (NSString *subPath in subPathArr) {
        filePath = [[self cachesDic] stringByAppendingPathComponent:subPath];
        //删除子文件夹
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
        if (error) {
            return NO;
        }
    }
    return YES;

}

//获取文件大小
+ (NSInteger)getFilePathSize:(NSString *)filePath
{
    BOOL isDirectory = NO;
    BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
    
    if (!isExist) {
        return -1;
    }
    NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
    NSInteger size = [dict[@"NSFileSize"] integerValue];
    
    return size;
}

+ (NSInteger)getFileSizeWithUrl:(NSURL *)fileUrl {
    if ([fileUrl isFileURL]) {
        NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileUrl path] error:nil];
        return [dict[@"NSFileSize"] integerValue];
    } else {
        NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileUrl absoluteString] error:nil];
        return [dict[@"NSFileSize"] integerValue];
    }
}

+ (NSInteger)getFilePathDicSize:(NSString *)dic{
    
    // 获取“path”文件夹下的所有文件
    NSArray *subPathArr = [[NSFileManager defaultManager] subpathsAtPath:dic];
    
    NSString *filePath  = nil;
    NSInteger totleSize = 0;
    
    for (NSString *subPath in subPathArr){
        
        // 1. 拼接每一个文件的全路径
        filePath =[dic stringByAppendingPathComponent:subPath];
        // 2. 是否是文件夹，默认不是
        BOOL isDirectory = NO;
        // 3. 判断文件是否存在
        BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
        
        // 4. 以上判断目的是忽略不需要计算的文件
        if (!isExist || isDirectory || [filePath containsString:@".DS"]){
            // 过滤: 1. 文件夹不存在  2. 过滤文件夹  3. 隐藏文件
            continue;
        }
        
        // 5. 指定路径，获取这个路径的属性
        NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
        /**
         attributesOfItemAtPath: 文件夹路径
         该方法只能获取文件的属性, 无法获取文件夹属性, 所以也是需要遍历文件夹的每一个文件的原因
         */
        
        // 6. 获取每一个文件的大小
        NSInteger size = [dict[@"NSFileSize"] integerValue];
        
        // 7. 计算总大小
        totleSize += size;
    }
    
    //8. 将文件夹大小转换为 M/KB/B
//    NSString *totleStr = nil;
//    
//    if (totleSize > 1000 * 1000){
//        totleStr = [NSString stringWithFormat:@"%.2fM",totleSize / 1000.00f /1000.00f];
//        
//    }else if (totleSize > 1000){
//        totleStr = [NSString stringWithFormat:@"%.2fKB",totleSize / 1000.00f ];
//        
//    }else{
//        totleStr = [NSString stringWithFormat:@"%.2fB",totleSize / 1.00f];
//    }
//    
//    return totleStr;
    
    return totleSize;
}

+ (BOOL)clearFilePathDic:(NSString *)dic{
    
    //拿到path路径的下一级目录的子文件夹
//    NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
//
//    NSString *filePath = nil;
//    
//    NSError *error = nil;
//    
//    for (NSString *subPath in subPathArr)
//    {
//        filePath = [path stringByAppendingPathComponent:subPath];
//        
//        //删除子文件夹
//        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
//        if (error) {
//            return NO;
//        }
//    }
//    return YES;
    
    return [[NSFileManager defaultManager] removeItemAtPath:dic error:nil];
}

@end
